<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one community based on id
			get_community();
			break;
			
			case 2:
			//get all communities
			get_all_communities();
			break;

			case 3:
			//update community
			update_community();
			break;
			
			case 4:
			//delete community
			delete_community();
			break;
			
			case 5:
			//add community
			add_community();
			break;
			
			case 6:
			//search community
			search_community();
			break;

		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
			
			
	}
	
	//returns a single community
	function get_community(){
		include_once("community.php");
		
		$id=get_datan("id");
		$v=new community();
		$row=$v->get_community($id);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","community not found");
			echo "}";
			return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"community":{';
			echo jsonn("id",$id).",";
			echo jsons("communityName",$row['community_name']).",";
			echo jsonn("longitude",$row['longitude']).",";
			echo jsonn("latitude",$row['latitude']).",";
			echo jsonn("population",$row['population']).",";
			echo jsonn("household",$row['household']).",";
			echo jsonn("subdistrict_id",$row['subdistrict_id']);
			echo "}";
		echo "}";
	}
	
	//returns all communities
	function get_all_communities(){
		include_once("community.php");
		
		$v=new community();
		$v->get_all_community();
		$row=$v->fetch();
		if (!$row){
		echo "{";
		echo jsonn("result",0) .",";
		echo jsons("Message", "community not found");
		echo "}";
		return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"communities":';
			$s=Array();
			do{
			array_push($s, $row);
			$row=$v->fetch();
			}while($row);
			print_r(json_encode($s));
			echo "}";
			
	}
	
	//allows user to edit the community details
	function update_community(){
		$id=get_datan('id');
		$community_name=get_data('cn');
		$longitude=get_data('clo');
		$latitude=get_data('cla');
		$population=get_data('cp');
		$household=get_data('ch');
		$subdistrict_id=get_data('cs');
	
		if(!$id)
		{
		//return error message
		echo "{";
		echo jsonn("result",0). ",";
		echo jsons("message", "id not correct");
		echo "}";
		return;
		}
		include_once("community.php");
		$v=new community();
		$update=$v->update_community($latitude,$longitude,$population,$household,$community_name,$subdistrict_id,$id);
		if($update){
		echo "{";
		echo jsonn("result",1). ",";
		echo jsons("message","Updated");
		echo "}";
		}
		else{
		echo "{";
		echo jsonn("result",0). ",";
		echo jsons("message","id not correct");
		echo "}";
		echo mysql_error();
		}
	}
	
	//delete an existing community
	function delete_community(){
		$id=get_datan('id');
		if(!$id)
		{
		//return error message
		echo "{";
		echo jsonn("result",0). ",";
		echo jsons("message","id not correct");
		echo "}";
		return;
		}
		include_once("community.php");
		$v=new community();
		$delete=$v->delete_community($id);
		if($delete){
		echo "{";
		echo jsonn("result",1). ",";
		echo jsons("message","successfully deleted");
		echo "}";
		}
		else{
		echo "{";
		echo jsonn("result",0). ",";
		echo jsons("message","id not correct");
		echo "}";
		echo mysql_error();
		}
	}
	
	//add a new community
	function add_community(){
		$community_name=get_data('cn');
		$longitude=get_data('clo');
		$latitude=get_data('cla');
		$population=get_data('cp');
		$household=get_data('ch');
		$subdistrict_id=get_data('cs');
	
		include_once("community.php");
		$v=new community();
		$add=$v->add_community($latitude,$longitude,$population,$household,$community_name,$subdistrict_id);
		if($add){
		echo "{";
		echo jsonn("result",1). ",";
		echo jsons("message","id not correct");
		echo "}";
		}
		else{
		echo "{";
		echo jsonn("result",0). ",";
		echo jsons("message","id not correct");
		echo "}";
		echo mysql_error();
		}
	}
	
	//search for a community
	function search_community(){
		$community_name=get_data('cn');
		include_once("community.php");
		$v=new community();
		$row=$v->search_community($community_name);
	
	if (!$row){
		echo "{";
		echo jsonn("result",0) .",";
		echo jsons("Message","community not found");
		echo "}";
		return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"communities":';
			$s=Array();
			do{
			array_push($s, $row);
			$row=$v->fetch();
			}while($row);
			print_r(json_encode($s));
			echo "}";
			}
?>